﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Linq;
using System.Windows.Controls.Primitives;

namespace ImageView
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
    public partial class MainWindow : Window
	{
        private List<string> _supported;
        private List<string> _images;
        private string[] _videofmts;
        FrameworkElement _backelem;
        FrameworkElement _previtem;
        private bool _refresh;

		public MainWindow()
		{
			this.InitializeComponent();
            App.MainWin = this;
            App.Taskbar = new Win7Integrator();
            App.Shell = new WinShellWraper();
            _images = new List<string>();
            _supported = new List<string>
            {
                "*.jpg", "*.jpeg", "*.bmp", "*.png", "*.tiff", "*.wdp"
            };
            _videofmts = new string[]
            {
                "*.avi", "*.mpg", "*.mpeg", "*.mp4", "*.m4v"
            };
            _refresh = false;
            _supported.AddRange(_videofmts);
            if (App.Shell.WebPInstalled()) _supported.Add("*.webp");
            _backelem = VThums;
		}

        public bool IsVideo(string file)
        {
            string ext = "*" + Path.GetExtension(file);
            return _videofmts.Contains(ext);
        }

        private void FileOpen_Click(object sender, RoutedEventArgs e)
        {
            string _directory = App.Shell.OpenDirectory(Properties.Settings.Default.LastDir);
            if (string.IsNullOrEmpty(_directory)) return;
            string[] tmp;
            _images.Clear();
            if (App.Shell.IsVirtualPath(_directory)) MessageBox.Show("Virtual directories can't be opened", "Informaton", MessageBoxButton.OK, MessageBoxImage.Information);
            try
            {
                foreach (var type in _supported)
                {
                    tmp = Directory.GetFiles(_directory, type);
                    _images.AddRange(tmp);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Can't open folder.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            Properties.Settings.Default.LastDir = _directory;
            _images.Sort();
            SetView();
            _refresh = true;
            _previtem = _backelem;
            TransPres.ApplyTransition(_previtem, _backelem);
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Properties.Settings.Default.WindowLeft = this.Left;
            Properties.Settings.Default.WindowTop = this.Top;
            Properties.Settings.Default.WindowWidth = this.Width;
            Properties.Settings.Default.WindowHeight = this.Height;
            Properties.Settings.Default.Save();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Left = Properties.Settings.Default.WindowLeft;
            this.Top = Properties.Settings.Default.WindowTop;
            this.Width = Properties.Settings.Default.WindowWidth;
            this.Height = Properties.Settings.Default.WindowHeight;
        }

        private void SetView()
        {
            Random r = new Random();
            int index = r.Next(0, 4);
            TransRotat.Rotation = (FluidKit.Controls.Direction)index;
        }

        public void SwitchtoImageView(int index)
        {
            SetView();
            VImage.Images = _images.ToArray();
            VImage.StartIndex = index;
            TransPres.ApplyTransition(_backelem, VImage);
            if (_backelem is IViev)
            {
                IViev v = (IViev)_backelem;
                v.ClearItems();
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
            }
            _previtem = _backelem;
            _backelem = VImage;
        }

        public void SwitchtoVideoView(int index)
        {
            SetView();
            VVideo.OpenVideo(_images[index]);
            _previtem = _backelem;
            TransPres.ApplyTransition(_backelem, VVideo);
            _backelem = VVideo;
        }

        public void CloseVideoView()
        {
            SetView();
            TransPres.ApplyTransition(_backelem, _previtem);
            _backelem = _previtem;
        }

        private void ViewSwitch(object sender, RoutedEventArgs e)
        {
            Button s = (Button)sender;
            SetView();
            _previtem = _backelem;
            if (_backelem is ViewVideo)
            {
                (_backelem as ViewVideo).CloseVideo();
            }
            switch (s.Content.ToString())
            {
                case "Thumbnails":
                    TransPres.ApplyTransition(_backelem, VThums);
                    _backelem = VThums;
                    break;
                case "List":
                    TransPres.ApplyTransition(_backelem, VList);
                    _backelem = VList;
                    break;
                case "Flow":
                    TransPres.ApplyTransition(_backelem, VFlow);
                    _backelem = VFlow;
                    break;
            }
        }

        private void TransPres_TransitionCompleted(object sender, EventArgs e)
        {

            if (!_refresh && (_previtem == _backelem || _previtem == VVideo)) return;
            if (_backelem == VImage)
            {
                if (_previtem is IViev)
                {
                    IViev v = (IViev)_previtem;
                    v.ClearItems();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                }
                return;
            }
            if (_backelem is IViev)
            {
                if (_previtem is IViev)
                {
                    IViev old = (IViev)_previtem;
                    old.ClearItems();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                }
                IViev v = (IViev)_backelem;
                v.ClearItems();
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                v.Fill(_images.ToArray());
                if (_refresh) _refresh = false;
            }
        }
	}
}